package com.ycy.temperature.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.jude.utils.JUtils;
import com.ycy.temperature.R;
import com.ycy.temperature.bean.DynamicBean;
import com.ycy.temperature.bean.VerificationBean;
import com.ycy.temperature.date.net.UserNetWorks;
import com.ycy.temperature.view.view.RoundImageView;
import com.zhy.autolayout.AutoLinearLayout;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.List;

import rx.Subscriber;

public class CircleAdapter extends RecyclerView.Adapter<CircleAdapter.ViewHolder> {

    private List<DynamicBean.DataBean> mCateList;
    private Context mContext;

    public CircleAdapter(List<DynamicBean.DataBean> list, Context mContext) {
        this.mCateList = list;
        this.mContext = mContext;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        AutoLinearLayout LayoutComment;
        AutoLinearLayout LayoutFabulous;
        RoundImageView ImageHeadImage;
        TextView TextNickName;
        TextView TextReleaseaddress;
        TextView TextContent;
        TextView TextCreatetime;
        TextView TextNews;
        TextView TextFabulous;
        ImageView ImageOne;
        ImageView ImageTwe;
        ImageView ImageThree;
        ImageView ImageFour;
        ImageView ImageFive;
        ImageView ImageSix;
        ImageView ImageSeven;
        ImageView ImageEight;
        ImageView ImageNine;
        ImageView ImageFabulous;

        public ViewHolder(View itemView) {
            super(itemView);
            ImageHeadImage = itemView.findViewById(R.id.Image_HeadImage);
            TextNickName = itemView.findViewById(R.id.Text_NickName);
            TextReleaseaddress = itemView.findViewById(R.id.Text_Releaseaddress);
            TextContent = itemView.findViewById(R.id.Text_Content);
            TextCreatetime = itemView.findViewById(R.id.Text_Createtime);
            TextNews = itemView.findViewById(R.id.Text_News);
            TextFabulous = itemView.findViewById(R.id.Text_Fabulous);
            ImageOne = itemView.findViewById(R.id.Image_One);
            ImageTwe = itemView.findViewById(R.id.Image_Twe);
            ImageThree = itemView.findViewById(R.id.Image_Three);
            ImageFour = itemView.findViewById(R.id.Image_Four);
            ImageFive = itemView.findViewById(R.id.Image_Five);
            ImageSix = itemView.findViewById(R.id.Image_Six);
            ImageSeven = itemView.findViewById(R.id.Image_Seven);
            ImageEight = itemView.findViewById(R.id.Image_Eight);
            ImageNine = itemView.findViewById(R.id.Image_Nine);
            ImageFabulous = itemView.findViewById(R.id.Image_Fabulous);
            LayoutComment = itemView.findViewById(R.id.Layout_Comment);
            LayoutFabulous = itemView.findViewById(R.id.Layout_Fabulous);
        }

    }

    public void clearList() {
        mCateList.clear();
        notifyDataSetChanged();
    }



    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_circle, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
        holder.setIsRecyclable(false);
        final DynamicBean.DataBean cate = mCateList.get(position);
        Glide.with(mContext)
                .load("http://" + cate.getActivityUserMessage().getHeadimage())
                .apply(new RequestOptions()
                        .centerCrop()
                        .placeholder(R.mipmap.ic_launcher)
                        .error(R.mipmap.ic_launcher)
                        .diskCacheStrategy(DiskCacheStrategy.ALL))
                .into(holder.ImageHeadImage);
        holder.TextNickName.setText(cate.getActivityUserMessage().getNickname());
        holder.TextReleaseaddress.setText(cate.getReleaseaddress());
        holder.TextContent.setText(cate.getContent());
        holder.TextNews.setText(cate.getCommentcount());
        holder.TextFabulous.setText(cate.getPraisecount());
        holder.TextCreatetime.setText(getDate(cate.getCreatetime()));
        if (cate.getPraiseStatus().equals("0")) {
            holder.ImageFabulous.setImageResource(R.mipmap.image_fabulous);
        } else if (cate.getPraiseStatus().equals("1")) {
            holder.ImageFabulous.setImageResource(R.mipmap.image_fabulous_true);
        }
        holder.LayoutFabulous.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getFabulous(cate.getActivityUserMessage().getUid(), cate.getId(), holder.ImageFabulous, holder.TextFabulous, cate);
            }
        });

        String[] arr = cate.getCircleimage().split(",");
        List<String> CirleList = Arrays.asList(arr);
        for (int i = 0; i < CirleList.size(); i++) {
            if (i == 0) {
                holder.ImageOne.setVisibility(View.VISIBLE);
                Glide.with(mContext)
                        .load("http://" + CirleList.get(i))
                        .apply(new RequestOptions()
                                .centerCrop()
                                .placeholder(R.mipmap.ic_launcher)
                                .error(R.mipmap.ic_launcher)
                                .diskCacheStrategy(DiskCacheStrategy.ALL))
                        .into(holder.ImageOne);
            } else if (i == 1) {
                holder.ImageTwe.setVisibility(View.VISIBLE);
                Glide.with(mContext)
                        .load("http://" + CirleList.get(i))
                        .apply(new RequestOptions()
                                .centerCrop()
                                .placeholder(R.mipmap.ic_launcher)
                                .error(R.mipmap.ic_launcher)
                                .diskCacheStrategy(DiskCacheStrategy.ALL))
                        .into(holder.ImageTwe);
            } else if (i == 2) {
                holder.ImageThree.setVisibility(View.VISIBLE);
                Glide.with(mContext)
                        .load("http://" + CirleList.get(i))
                        .apply(new RequestOptions()
                                .centerCrop()
                                .placeholder(R.mipmap.ic_launcher)
                                .error(R.mipmap.ic_launcher)
                                .diskCacheStrategy(DiskCacheStrategy.ALL))
                        .into(holder.ImageThree);
            } else if (i == 3) {
                holder.ImageFour.setVisibility(View.VISIBLE);
                Glide.with(mContext)
                        .load("http://" + CirleList.get(i))
                        .apply(new RequestOptions()
                                .centerCrop()
                                .placeholder(R.mipmap.ic_launcher)
                                .error(R.mipmap.ic_launcher)
                                .diskCacheStrategy(DiskCacheStrategy.ALL))
                        .into(holder.ImageFour);
            } else if (i == 4) {
                holder.ImageFive.setVisibility(View.VISIBLE);
                Glide.with(mContext)
                        .load("http://" + CirleList.get(i))
                        .apply(new RequestOptions()
                                .centerCrop()
                                .placeholder(R.mipmap.ic_launcher)
                                .error(R.mipmap.ic_launcher)
                                .diskCacheStrategy(DiskCacheStrategy.ALL))
                        .into(holder.ImageFive);
            } else if (i == 5) {
                holder.ImageSix.setVisibility(View.VISIBLE);
                Glide.with(mContext)
                        .load("http://" + CirleList.get(i))
                        .apply(new RequestOptions()
                                .centerCrop()
                                .placeholder(R.mipmap.ic_launcher)
                                .error(R.mipmap.ic_launcher)
                                .diskCacheStrategy(DiskCacheStrategy.ALL))
                        .into(holder.ImageSix);
            } else if (i == 6) {
                holder.ImageSeven.setVisibility(View.VISIBLE);
                Glide.with(mContext)
                        .load("http://" + CirleList.get(i))
                        .apply(new RequestOptions()
                                .centerCrop()
                                .placeholder(R.mipmap.ic_launcher)
                                .error(R.mipmap.ic_launcher)
                                .diskCacheStrategy(DiskCacheStrategy.ALL))
                        .into(holder.ImageSeven);
            } else if (i == 7) {
                holder.ImageEight.setVisibility(View.VISIBLE);
                Glide.with(mContext)
                        .load("http://" + CirleList.get(i))
                        .apply(new RequestOptions()
                                .centerCrop()
                                .placeholder(R.mipmap.ic_launcher)
                                .error(R.mipmap.ic_launcher)
                                .diskCacheStrategy(DiskCacheStrategy.ALL))
                        .into(holder.ImageEight);
            } else if (i == 8) {
                holder.ImageNine.setVisibility(View.VISIBLE);
                Glide.with(mContext)
                        .load("http://" + CirleList.get(i))
                        .apply(new RequestOptions()
                                .centerCrop()
                                .placeholder(R.mipmap.ic_launcher)
                                .error(R.mipmap.ic_launcher)
                                .diskCacheStrategy(DiskCacheStrategy.ALL))
                        .into(holder.ImageNine);
            }
        }
    }

    private String getDate(String date) {
        long day = 0;
        long hour = 0;
        long min = 0;
        long sec = 0;
        long diff;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        try {
            diff = System.currentTimeMillis() - sdf.parse(date).getTime();
            day = diff / (24 * 60 * 60);
            hour = (diff / (60 * 60) - day * 24);
            min = ((diff / 60) - day * 24 * 60 - hour * 60);
            sec = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (day != 0) {
            if (day >= 2) {
                return date;
            } else {
                return day + "天前";
            }
        }
        if (hour != 0) return hour + "小时前";
        if (min != 0) return min + "分钟前";
        if (sec != 0) return "1分钟前";
        return "0秒";

    }

    @Override
    public int getItemCount() {
        return mCateList.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void getFabulous(String Createuserid, String Circleid, final ImageView mImage, final TextView mText, final DynamicBean.DataBean mDateBean) {
        UserNetWorks.getFabulous(Createuserid, Circleid, new Subscriber<VerificationBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
                JUtils.Toast("网络错误");
            }

            @Override
            public void onNext(VerificationBean verificationBean) {
                if (verificationBean.getResponseState().equals("1")) {
                    if (mDateBean.getPraiseStatus().equals("0")) {
                        mDateBean.setPraiseStatus("1");
                        mImage.setImageResource(R.mipmap.image_fabulous_true);
                        mText.setText((Integer.valueOf(mText.getText().toString()) + 1) + "");
                    } else if (mDateBean.getPraiseStatus().equals("1")) {
                        mDateBean.setPraiseStatus("0");
                        mImage.setImageResource(R.mipmap.image_fabulous);
                        mText.setText((Integer.valueOf(mText.getText().toString()) - 1) + "");
                    }
                }
            }
        });
    }


}
